from django import forms
from app01 import models
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator


class RegisterForm(forms.ModelForm):
    # 这里定义的字段名如果名字==models字段名则修改显示，没有，则新增显示
    phone = forms.CharField(
        label='您的手机号',  # 修改字段显示
        validators=[
            RegexValidator(r'1(3|4|5|6|7|8|9)\d{9}$', '手机号格式错误'),  # 正则判断输入
        ],
    )

    pwd = forms.CharField(label='密码',
                          widget=forms.PasswordInput(attrs=dict(placeholder='请输入密码')))  # 密码使用秘闻输入
    confirm_pwd = forms.CharField(label='确认密码', widget=forms.PasswordInput())  # 生成models中没有的字段
    code = forms.CharField(label='验证码')

    class Meta:
        model = models.UserInfo
        fields = ['username','email','pwd','confirm_pwd','phone','code']  # 自定义code排序
        # fields = '__all__'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = f'请输入{field.label}'
